<?php

/*============================================
Classe McsMonitoring
============================================*/

class McsMonitoring extends ModelObject{

	protected static $cached_object_list= array();
	protected $MEAS_NAME;
	protected $RESSOURCES;
	protected $NB_EXEC;
	protected $PERIOD;
	protected $PRIORITY;
	protected $MAX_DURATION;
	protected $OFFSET_DEMARRAGE;
	protected $LAUNCH_STATEMENT;
	protected $SIMU_PROCESSING_TASK;
	protected $SIMU_EXEC_CTRLE;

	protected function __construct( $MEAS_NAME,$RESSOURCES,$NB_EXEC,$PERIOD,$PRIORITY,$MAX_DURATION,$OFFSET_DEMARRAGE,$LAUNCH_STATEMENT,$SIMU_PROCESSING_TASK,$SIMU_EXEC_CTRLE){
		try{
			
			$this->MEAS_NAME = $MEAS_NAME;
			$this->RESSOURCES = $RESSOURCES;
			$this->NB_EXEC = $NB_EXEC;
			$this->PERIOD = $PERIOD;
			$this->PRIORITY = $PRIORITY;
			$this->MAX_DURATION = $MAX_DURATION;
			$this->OFFSET_DEMARRAGE = $OFFSET_DEMARRAGE;
			$this->LAUNCH_STATEMENT = $LAUNCH_STATEMENT;
			$this->SIMU_PROCESSING_TASK = $SIMU_PROCESSING_TASK;
			$this->SIMU_EXEC_CTRLE = $SIMU_EXEC_CTRLE;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getMEASNAME(){
		return $this->MEAS_NAME;
	}

	public function getRESSOURCES(){
		return $this->RESSOURCES;
	}

	public function getNBEXEC(){
		return $this->NB_EXEC;
	}

	public function getPERIOD(){
		return $this->PERIOD;
	}

	public function getPRIORITY(){
		return $this->PRIORITY;
	}

	public function getMAXDURATION(){
		return $this->MAX_DURATION;
	}

	public function getOFFSETDEMARRAGE(){
		return $this->OFFSET_DEMARRAGE;
	}

	public function getLAUNCHSTATEMENT(){
		return $this->LAUNCH_STATEMENT;
	}

	public function getSIMUPROCESSINGTASK(){
		return $this->SIMU_PROCESSING_TASK;
	}

	public function getSIMUEXECCTRLE(){
		return $this->SIMU_EXEC_CTRLE;
	}

	public function setRESSOURCES( $RESSOURCES){
		try{
			$temp = $this->RESSOURCES;
			if($this->RESSOURCES !== $RESSOURCES){
				$this->RESSOURCES = $RESSOURCES;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->RESSOURCES = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setRESSOURCES() a échouée",0,$e);
		}
	}

	public function setNBEXEC( $NB_EXEC){
		try{
			$temp = $this->NB_EXEC;
			if($this->NB_EXEC !== $NB_EXEC){
				$this->NB_EXEC = $NB_EXEC;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->NB_EXEC = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setNBEXEC() a échouée",0,$e);
		}
	}

	public function setPERIOD( $PERIOD){
		try{
			$temp = $this->PERIOD;
			if($this->PERIOD !== $PERIOD){
				$this->PERIOD = $PERIOD;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->PERIOD = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setPERIOD() a échouée",0,$e);
		}
	}

	public function setPRIORITY( $PRIORITY){
		try{
			$temp = $this->PRIORITY;
			if($this->PRIORITY !== $PRIORITY){
				$this->PRIORITY = $PRIORITY;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->PRIORITY = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setPRIORITY() a échouée",0,$e);
		}
	}

	public function setMAXDURATION( $MAX_DURATION){
		try{
			$temp = $this->MAX_DURATION;
			if($this->MAX_DURATION !== $MAX_DURATION){
				$this->MAX_DURATION = $MAX_DURATION;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MAX_DURATION = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMAXDURATION() a échouée",0,$e);
		}
	}

	public function setOFFSETDEMARRAGE( $OFFSET_DEMARRAGE){
		try{
			$temp = $this->OFFSET_DEMARRAGE;
			if($this->OFFSET_DEMARRAGE !== $OFFSET_DEMARRAGE){
				$this->OFFSET_DEMARRAGE = $OFFSET_DEMARRAGE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->OFFSET_DEMARRAGE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setOFFSETDEMARRAGE() a échouée",0,$e);
		}
	}

	public function setLAUNCHSTATEMENT( $LAUNCH_STATEMENT){
		try{
			$temp = $this->LAUNCH_STATEMENT;
			if($this->LAUNCH_STATEMENT !== $LAUNCH_STATEMENT){
				$this->LAUNCH_STATEMENT = $LAUNCH_STATEMENT;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->LAUNCH_STATEMENT = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setLAUNCHSTATEMENT() a échouée",0,$e);
		}
	}

	public function setSIMUPROCESSINGTASK( $SIMU_PROCESSING_TASK){
		try{
			$temp = $this->SIMU_PROCESSING_TASK;
			if($this->SIMU_PROCESSING_TASK !== $SIMU_PROCESSING_TASK){
				$this->SIMU_PROCESSING_TASK = $SIMU_PROCESSING_TASK;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SIMU_PROCESSING_TASK = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSIMUPROCESSINGTASK() a échouée",0,$e);
		}
	}

	public function setSIMUEXECCTRLE( $SIMU_EXEC_CTRLE){
		try{
			$temp = $this->SIMU_EXEC_CTRLE;
			if($this->SIMU_EXEC_CTRLE !== $SIMU_EXEC_CTRLE){
				$this->SIMU_EXEC_CTRLE = $SIMU_EXEC_CTRLE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SIMU_EXEC_CTRLE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSIMUEXECCTRLE() a échouée",0,$e);
		}
	}

	public static function create( $MEAS_NAME,$RESSOURCES,$NB_EXEC,$PERIOD,$PRIORITY,$MAX_DURATION,$OFFSET_DEMARRAGE,$LAUNCH_STATEMENT,$SIMU_PROCESSING_TASK,$SIMU_EXEC_CTRLE){
		try{
			$mcsMonitoring = new McsMonitoring($MEAS_NAME, $RESSOURCES, $NB_EXEC, $PERIOD, $PRIORITY, $MAX_DURATION, $OFFSET_DEMARRAGE, $LAUNCH_STATEMENT, $SIMU_PROCESSING_TASK, $SIMU_EXEC_CTRLE);
			$mcsMonitoring->insert();
			McsMonitoring::cacheObject($mcsMonitoring);
			return $mcsMonitoring;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type McsMonitoring a échouée",0,$e);
		}
	}

	public static function get( $MEAS_NAME){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($MEAS_NAME) || $MEAS_NAME === "") {
				return null;
			}
			if(McsMonitoring::existsInCache($MEAS_NAME)) {
				return McsMonitoring::getFromCache($MEAS_NAME);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `mcs_monitoring` WHERE `MEAS_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$MEAS_NAME));
			if ($row = $stmt->fetch()){
				$mcsMonitoring = new McsMonitoring( $row['MEAS_NAME'],$row['RESSOURCES'],$row['NB_EXEC'],$row['PERIOD'],$row['PRIORITY'],$row['MAX_DURATION'],$row['OFFSET_DEMARRAGE'],$row['LAUNCH_STATEMENT'],$row['SIMU_PROCESSING_TASK'],$row['SIMU_EXEC_CTRLE']);
				$mcsMonitoring->id =  $row['MEAS_NAME'];
				$dbh->commit();
				McsMonitoring::cacheObject($mcsMonitoring);
				return $mcsMonitoring;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $MEAS_NAME . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `mcs_monitoring` SET `RESSOURCES` = :RESSOURCES, `NB_EXEC` = :NB_EXEC, `PERIOD` = :PERIOD, `PRIORITY` = :PRIORITY, `MAX_DURATION` = :MAX_DURATION, `OFFSET_DEMARRAGE` = :OFFSET_DEMARRAGE, `LAUNCH_STATEMENT` = :LAUNCH_STATEMENT, `SIMU_PROCESSING_TASK` = :SIMU_PROCESSING_TASK, `SIMU_EXEC_CTRLE` = :SIMU_EXEC_CTRLE WHERE `MEAS_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':RESSOURCES'=>$this->RESSOURCES, ':NB_EXEC'=>$this->NB_EXEC, ':PERIOD'=>$this->PERIOD, ':PRIORITY'=>$this->PRIORITY, ':MAX_DURATION'=>$this->MAX_DURATION, ':OFFSET_DEMARRAGE'=>$this->OFFSET_DEMARRAGE, ':LAUNCH_STATEMENT'=>$this->LAUNCH_STATEMENT, ':SIMU_PROCESSING_TASK'=>$this->SIMU_PROCESSING_TASK, ':SIMU_EXEC_CTRLE'=>$this->SIMU_EXEC_CTRLE, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `mcs_monitoring` WHERE `MEAS_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			McsMonitoring::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `mcs_monitoring` ( `RESSOURCES`, `NB_EXEC`, `PERIOD`, `PRIORITY`, `MAX_DURATION`, `OFFSET_DEMARRAGE`, `LAUNCH_STATEMENT`, `SIMU_PROCESSING_TASK`, `SIMU_EXEC_CTRLE`) VALUES (:RESSOURCES, :NB_EXEC, :PERIOD, :PRIORITY, :MAX_DURATION, :OFFSET_DEMARRAGE, :LAUNCH_STATEMENT, :SIMU_PROCESSING_TASK, :SIMU_EXEC_CTRLE)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':RESSOURCES'=>$this->RESSOURCES, ':NB_EXEC'=>$this->NB_EXEC, ':PERIOD'=>$this->PERIOD, ':PRIORITY'=>$this->PRIORITY, ':MAX_DURATION'=>$this->MAX_DURATION, ':OFFSET_DEMARRAGE'=>$this->OFFSET_DEMARRAGE, ':LAUNCH_STATEMENT'=>$this->LAUNCH_STATEMENT, ':SIMU_PROCESSING_TASK'=>$this->SIMU_PROCESSING_TASK, ':SIMU_EXEC_CTRLE'=>$this->SIMU_EXEC_CTRLE));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `mcs_monitoring`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(McsMonitoring::existsInCache($row['MEAS_NAME'])){
					$obj = McsMonitoring::getFromCache($row['MEAS_NAME']);
				}
				else{
					$obj = new McsMonitoring( $row['MEAS_NAME'],$row['RESSOURCES'],$row['NB_EXEC'],$row['PERIOD'],$row['PRIORITY'],$row['MAX_DURATION'],$row['OFFSET_DEMARRAGE'],$row['LAUNCH_STATEMENT'],$row['SIMU_PROCESSING_TASK'],$row['SIMU_EXEC_CTRLE']);
				}
				if(!is_null($obj)){
					$obj->id = $row['MEAS_NAME'];
					McsMonitoring::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type McsMonitoring a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `mcs_monitoring`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type McsMonitoring a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `mcs_monitoring`';
			$sql_select = 'SELECT MEAS_NAME FROM `mcs_monitoring`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = McsMonitoring::getObjectFromCache($row['MEAS_NAME']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						McsMonitoring::removeObjectFromCache($row['MEAS_NAME']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type McsMonitoring a échouée",0,$e);
		}
	}

}